import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
/* 饿了么UI按需导入与自动加载 */
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
/* src 别名设置 */
import path from 'path'
/* 引入全局SVG图标设置 */
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
/* mock数据 */
import { ConfigEnv } from 'vite'
import { viteMockServe } from 'vite-plugin-mock'
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }: ConfigEnv) => {
  /* 获取各种运行环境对应的变量 */
  const env = loadEnv(mode, process.cwd())
  return {
    plugins: [
      // ...
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
      /* 引入全局SVG图标设置 */
      createSvgIconsPlugin({
        /*  Specify the icon folder to be cached */
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        /*  Specify symbolId format */
        symbolId: 'icon-[dir]-[name]',
      }),
      viteMockServe({
        // default
        mockPath: 'mock',
        localEnabled: command === 'serve',
      }),
    ],
    /* 别名设置 */
    resolve: {
      alias: {
        '@': path.resolve('./src'), // 相对路径别名配置，使用 @ 代替 src
      },
    },
    /* scss全局变量配置 */
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: '@import "./src/style/var.scss";',
        },
      },
    },
    /* 代理服务器 */
    server: {
      /* 代理服务器 */
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
  }
})
